//
//  PayrollAgencyViewController.m
//  ActSistant
//
//  Created by Hieu Nguyen Trung on 3/6/13.
//  Copyright (c) 2013 Savvycom. All rights reserved.
//

#import "PayrollAgencyViewController.h"
#import "ActSettingViewController.h"
#import "ActPaymentSelectionViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "ActPayrollCalculatorFeeViewController.h"
#import "PayrollAgencyCell.h"
#import "AddAgentCell.h"

@interface PayrollAgencyViewController ()

@end

@implementation PayrollAgencyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initializeViews];
}

-(void) initializeViews{
    [self.navigationView.btnBack addTarget:self action:@selector(btnBackClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView.btnRight1 addTarget:self action:@selector(btnSettingClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tableView setTableFooterView:[[[UIView alloc] initWithFrame:CGRectZero] autorelease]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release];
    [_navigationView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTableView:nil];
    [self setNavigationView:nil];
    [super viewDidUnload];
}

#pragma mark - Actions Handle
-(void) btnBackClicked{
    [self.navigationController popViewControllerAnimated:true];
}

-(void) btnSettingClicked{
    ActSettingViewController *settingVC = [[[ActSettingViewController alloc] init] autorelease];
    [self.navigationController pushViewController:settingVC animated:true];
}

#pragma mark - UITableView Methods Implementation
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0){
        return 1;
    }
    return [PAYROLL_LABELS_STRING_ARRAY count];
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 54;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == 0 && indexPath.row == 0){
//        return 64;
//    }
    return 44;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 54)] autorelease];
    [view setBackgroundColor:[UIColor whiteColor]];
    
    UIView *containerView = [[[UIView alloc] init] autorelease];
    [containerView setBackgroundColor:[UIColor colorWithRed:1 green:86/255.0 blue:86/255.0 alpha:1]];
    
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:UIFontSystemBold(20)];
    [label setTextColor:[UIColor whiteColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    
    if (section == 0){
        [containerView setFrame:CGRectMake(0, 0, 320, 44)];
        [label setText:@"Agency & Manager"];
    }
    else if (section == 1){
        [containerView setFrame:CGRectMake(0, 10, 320, 44)];
        [label setText:@"Select Payroll"];
    }
    
    [containerView addSubview:label];
    [view addSubview:containerView];
    return view;
}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 44;
    if (scrollView.contentOffset.y <= sectionHeaderHeight&& scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"Cell %d%d", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        
    }
    if (indexPath.section == 0 && indexPath.row == 0){
        cell = [self cellForAddAgency];
    }
    else{
        NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"PayrollAgencyCell" owner:nil options:nil];
        cell = objectsInXIB[0];
        [((PayrollAgencyCell *) cell).lblTitle setText:PAYROLL_LABELS_STRING_ARRAY[indexPath.row]];
    }
    
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    if (indexPath.section == 0){
        ActPayrollCalculatorFeeViewController *calFeeVC = [[[ActPayrollCalculatorFeeViewController alloc] initWithType:ActPayrollCaculatorTypeFree] autorelease];
        [self.navigationController pushViewController:calFeeVC animated:true];
        [[tableView cellForRowAtIndexPath:indexPath] setSelected:false];
        return;
    }
    switch (indexPath.row) {
        case 0:{
            ActPaymentSelectionViewController *paymentVC = [[[ActPaymentSelectionViewController alloc] initWithPaymentSelectionType:ActPaymentSelectionTypeCAPS] autorelease];
            [self.navigationController pushViewController:paymentVC animated:true];
            [[tableView cellForRowAtIndexPath:indexPath] setSelected:false];
            break;
        }
        case 1:{
            ActPaymentSelectionViewController *paymentVC = [[[ActPaymentSelectionViewController alloc] initWithPaymentSelectionType:ActPaymentSelectionTypeEntertainment] autorelease];
            [self.navigationController pushViewController:paymentVC animated:true];
            [[tableView cellForRowAtIndexPath:indexPath] setSelected:false];
            break;
        }
        case 2:{
            ActPaymentSelectionViewController *paymentVC = [[[ActPaymentSelectionViewController alloc] initWithPaymentSelectionType:ActPaymentSelectionTypeCentreCasting] autorelease];
            [self.navigationController pushViewController:paymentVC animated:true];
            [[tableView cellForRowAtIndexPath:indexPath] setSelected:false];
            break;
        }
        default:
            break;
    }
}

-(UITableViewCell *) cellForAddAgency{
//    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
//
//    
//    UILabel *lblAdd = [[[UILabel alloc] initWithFrame:CGRectMake(50, 0, 220, 30)] autorelease];
//    [lblAdd setText:@"Add Agent"];
//    [lblAdd setTextColor:[UIColor darkGrayColor]];
//    [lblAdd setTextAlignment:NSTextAlignmentCenter];
//    [lblAdd setFont:UIFontSystem(16)];
//    
//    UILabel *lblFee = [[[UILabel alloc] initWithFrame:CGRectMake(50, 25, 220, 14)] autorelease];
//    [lblFee setText:@"Fee Calculator"];
//    [lblFee setTextAlignment:NSTextAlignmentCenter];
//    [lblFee setTextColor:[UIColor lightGrayColor]];
//    [lblFee setFont:UIFontSystem(10)];
//    
//    UIImageView *imgView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btnFeeAdd"]] autorelease];
//    [imgView setFrame:CGRectMake(5, 2, 50, 40)];
//    
//    [cell addSubview:imgView];
//    [cell addSubview:lblFee];
//    [cell addSubview:lblAdd];
//    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    AddAgentCell *cell = [[NSBundle mainBundle] loadNibNamed:@"PayrollAgencyCell" owner:nil options:nil][3];
    [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [cell.layer setCornerRadius:8];
    [cell.layer setBorderWidth:0.5];
    [cell setClipsToBounds:true];


    return cell;
}
@end
